package com.yxjwzpx.management.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.yxjwzpx.management.domain.Ship;
import com.yxjwzpx.management.mapper.MaintainCompanyMapper;
import com.yxjwzpx.management.mapper.ShipMapper;
import com.yxjwzpx.management.mapper.ShipOwnerMapper;
import com.yxjwzpx.management.service.IShipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 船舶信息Service业务层处理
 * 
 * @author yxjwzpx
 * @date 2025-04-09
 */
@Service
public class ShipServiceImpl implements IShipService 
{
    @Autowired
    private ShipMapper shipMapper;

    @Autowired
    private ShipOwnerMapper shipOwnerMapper;

    @Autowired
    private MaintainCompanyMapper maintainCompanyMapper;

    @Override
    public int countShips(Long ownerId, Long companyId) {
        return shipMapper.countAll(ownerId, companyId);
    }



    /**
     * 查询船舶信息
     * 
     * @param shipId 船舶信息主键
     * @return 船舶信息
     */
    @Override
    public Ship selectShipByShipId(Long shipId)
    {
        return shipMapper.selectShipByShipId(shipId);
    }

    /**
     * 查询船舶信息列表
     * 
     * @param ship 船舶信息
     * @return 船舶信息
     */

    @Override
    public List<Ship> selectShipList(Ship ship) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        List<SysRole> roles = user.getRoles();
        if ("OWNER".equals(roles.get(0).getRoleKey())) {
            // 当前是船家用户 → 查询 owner_id
            Long ownerId = shipOwnerMapper.selectOwnerIdByUserId(user.getUserId());
            if (ownerId != null) {
                ship.setOwnerId(ownerId); // ship 表中字段
            } else {
                return new ArrayList<>(); // 没绑定船家信息，不返回任何数据
            }

        } else if ("COMPANY".equals(roles.get(0).getRoleKey())) {
            // 当前是维保公司用户 → 查询 company_id
            Long companyId = maintainCompanyMapper.selectCompanyIdByUserId(user.getUserId());
            if (companyId != null) {
                ship.setCompanyId(companyId); // ship 表中字段
            } else {
                return new ArrayList<>();
            }
        }
        // 管理员或其他角色：不设置任何过滤条件
        return shipMapper.selectShipList(ship);
    }


//    @Override
//    public List<Ship> selectShipList(Ship ship) {
//
//        return shipMapper.selectShipList(ship);
//    }


    /**
     * 新增船舶信息
     * 
     * @param ship 船舶信息
     * @return 结果
     */
    @Override
    public int insertShip(Ship ship)
    {
        ship.setCreateTime(DateUtils.getNowDate());
        return shipMapper.insertShip(ship);
    }

    /**
     * 修改船舶信息
     * 
     * @param ship 船舶信息
     * @return 结果
     */
    @Override
    public int updateShip(Ship ship)
    {
        ship.setUpdateTime(DateUtils.getNowDate());
        return shipMapper.updateShip(ship);
    }

    /**
     * 批量删除船舶信息
     * 
     * @param shipIds 需要删除的船舶信息主键
     * @return 结果
     */
    @Override
    public int deleteShipByShipIds(Long[] shipIds)
    {
        return shipMapper.deleteShipByShipIds(shipIds);
    }

    /**
     * 删除船舶信息信息
     * 
     * @param shipId 船舶信息主键
     * @return 结果
     */
    @Override
    public int deleteShipByShipId(Long shipId)
    {
        return shipMapper.deleteShipByShipId(shipId);
    }
}
